package com.itheima.reggie.controller;


import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类和套餐分类
     * @param category
     * @param session
     * @return
     */
    @PostMapping
    public R<String> addCategory(@RequestBody Category category, HttpSession session){
        //设置添加员工和修改员工
        Long id = (Long) session.getAttribute("employee");
        category.setCreateUser(id);
        category.setUpdateUser(id);
        categoryService.addCategory(category);
        return R.success("");
    }

    /**
     * 分页查询菜品分类和套餐分类
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<Category>> selectByPage(Integer page,Integer pageSize){
        Page<Category> categoryPage = categoryService.selectByPage(page, pageSize);
        return R.success(categoryPage);
    }


    /**
     * 删除分类
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete( Long id){
        categoryService.delete(id);
        return R.success("");
    }
    /**
     * 修改分类
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category,HttpSession session){
        //设置修改者
        Long id = (Long) session.getAttribute("employee");
        category.setUpdateUser(id);
        categoryService.update(category);
        return R.success("");
    }
    @GetMapping("/list")
    public R<List<Category>> selectByType(Integer type){
        List<Category> categoryList = categoryService.selectByType(type);
        return R.success(categoryList);

    }



}
